package com.c1202m.dao;

import com.c1202m.gui.BangThongKe;
import com.c1202m.gui.CustomMessageDialog;
import com.c1202m.hibernate.entities.GuiTien;
import com.c1202m.hibernate.entities.TaiKhoan;
import com.c1202m.util.DateUtil;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.hibernate.Query;
import org.hibernate.Session;

public class GuiTienDAO {

    public static Vector layToanBoDoiTuong() {
        Vector vt = new Vector();
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("select g.ngayGui, t.chuTK, t.soCMND, g.soTK, g.soTienGui from GuiTien g inner join g.taiKhoan t");
            list = query.list();
            for (Iterator it = list.iterator(); it.hasNext();) {
                Object[] o = (Object[]) it.next();
                Vector temp = new Vector();
                temp.add(o[0]);
                temp.add(o[1]);
                temp.add(o[2]);
                temp.add(o[3]);
                temp.add(o[4]);
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static Vector layDoiTuongTheoSoTk(String soTK) {
        Vector vt = new Vector();
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("from GuiTien a where a.soTK = ?");
            query.setString(0, soTK);
            list = query.list();
            for (Object obj : list) {
                GuiTien guiTien = (GuiTien) obj;
                Vector temp = new Vector();
                temp.add(guiTien.getNgayGui());
                temp.add(guiTien.getSoTK());
                temp.add(guiTien.getSoTienGui());
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static Vector layDoiTuongTheoNgay() {
        Vector vt = new Vector();
        if (BangThongKe.jxDate.getDate() == null) {
            return vt;
        }
        List list = null;
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            Query query = session.createQuery("select g.ngayGui, t.chuTK, t.soCMND, g.soTK, g.soTienGui from GuiTien g inner join g.taiKhoan t where g.ngayGui between ? and ?");
            Date ngayRut = BangThongKe.jxDate.getDate();
            java.sql.Timestamp sqlDateFirstTime = new java.sql.Timestamp(DateUtil.getDayFirstTime(ngayRut).getTime());
            java.sql.Timestamp sqlDateLastTime = new java.sql.Timestamp(DateUtil.getDayLastTime(ngayRut).getTime());
            String strF = String.valueOf(sqlDateFirstTime);
            String strL = String.valueOf(sqlDateLastTime);

            query.setString(0, strF);
            query.setString(1, strL);

            list = query.list();
            for (Iterator it = list.iterator(); it.hasNext();) {
                Object[] o = (Object[]) it.next();
                Vector temp = new Vector();
                temp.add(o[0]);
                temp.add(o[1]);
                temp.add(o[2]);
                temp.add(o[3]);
                temp.add(o[4]);
                vt.add(temp);
            }
            session.getTransaction().commit();
            session.close();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return vt;
    }

    public static boolean guiTien(String soTK, double soTienGui) {
        Session session = DangNhapDAO.sessionFactory.openSession();
        try {
            session.getTransaction().begin();
            TaiKhoan taiKhoan = (TaiKhoan) session.get(TaiKhoan.class, soTK);
            double soDuTK = Double.valueOf(taiKhoan.getSoDuTK()) + Double.valueOf(soTienGui);
            taiKhoan.setSoDuTK(soDuTK);
            session.update(taiKhoan);
            GuiTien guiTien = new GuiTien();
            guiTien.setNgayGui(new Date());
            guiTien.setSoTK(soTK);
            guiTien.setSoTienGui(soTienGui);
            session.save(guiTien);
            session.getTransaction().commit();
            session.close();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        new CustomMessageDialog(null, true, "Oops...!", "An error occured!", CustomMessageDialog.MESSAGE);
        return false;
    }
}
